<!-- 模板界面,使用时全局替换
    regiType  为此页要维护的信息(例如department)
    regiTypeData 为此页面要维护的信息的数组(例如deptData) 
    registLevel    为此页面要维护的信息的一个成员(例如deptName之于department),作为示例使用
    -->
<template>
  <div>
    挂号界面
    <el-button type="primary" plain @click="addButtonClicked">添加</el-button>
    <el-button type="primary" plain @click="batchRemoveButtonClicked">批量删除</el-button>
    <!-- <el-button type="primary" plain @click="queryButtonClicked">查询</el-button>
    <el-button type="primary" plain @click="queryResetButtonClicked">重置</el-button>-->
    <el-table :data="regiTypeData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column>
        <!-- <el-checkbox></el-checkbox> -->
        <el-table-column type="selection" width="55"></el-table-column>
      </el-table-column>
      <!-- <el-table-column prop="date" label="日期" width="180"></el-table-column> -->
      <el-table-column prop="registLevel" label="名称" width="180"></el-table-column>
      <el-table-column prop="registCode" label="编码" width="180"></el-table-column>
      <el-table-column prop="registDefalut" label="是否默认" width="180"></el-table-column>
      <el-table-column prop="registCost" label="价格" width="180"></el-table-column>
      <el-table-column prop="registOrder" label="顺序号" width="180"></el-table-column>
      <!-- 其实在el-table下,书写的各个列的信息,然后会根据userData自动生成n行 -->
      <el-table-column align="right">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加和修改 -->
    <el-dialog title="添加" :visible.sync="addDialogVisible">
      <el-form :model="regiType" ref="form" label-width="100px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="regiType.registLevel"></el-input>
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="regiType.registCode"></el-input>
        </el-form-item>
        <el-form-item label="是否默认" prop="defalut">
          <el-radio v-model="regiType.registDefalut" label="不默认">不默认</el-radio>
          <el-radio v-model="regiType.registDefalut" label="默认">默认</el-radio>
        </el-form-item>
        <el-form-item label="价格" prop="code">
          <el-input v-model="regiType.registCost"></el-input>
        </el-form-item>
        <el-form-item label="顺序号" prop="order">
          <el-input v-model="regiType.registOrder"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPageNum"
        :page-sizes="[1, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalRecord"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  insert,
  update,
  deleteElem,
  getPage,
  getAll,
  getAllType
} from "@/api/regiType.js";
export default {
  mounted() {
    // 渲染完成
    this.loadPageData();
  },
  data() {
    return {
      // 复选框
      multipleSelection: [],
      // 分页
      pageSize: 1,
      totalRecord: 3,
      currentPageNum: 0,
      // 对话框
      addDialogVisible: false,
      queryDialogVisible: false,
      // 局部对象
      // 存储路径
      /* 补全信息 */
      regiType: {
        id: "",
        registLevel: "",
        registCode: "",
        registDefalut: "",
        registCost: "",
        registOrder: ""
      },

      // 对象数组
      regiTypeData: []
    };
  },
  methods: {
    addButtonClicked() {
      // 清空
      /* 补全信息 */
      this.regiType.id = "";
      this.regiType.registLevel = "";
      this.regiType.registCode = "";
      this.regiType.registDefalut = "";
      this.regiType.registCost = "";
      this.regiType.registOrder = "";
      this.updateFlag = false;
      this.addDialogVisible = true;
      this.loadPageData();
    },
    loadPageData() {
      // 这个函数会更新总个数和数组中的信息
      // 查询条件
      let pageParam = {
        currentPage: this.currentPageNum,
        pageSize: this.pageSize
      };
      console.log("send getPage: ", pageParam);
      getPage(pageParam).then(response => {
        if (response.data.code == 0) {
          this.currentPageNum = parseInt(response.data.data.current);
          this.regiTypeData = response.data.data.records;
          this.totalRecord = parseInt(response.data.data.total);
          console.log("getPage: ", this.regiTypeData);
        } else {
          console.log("getPage ERROR!!");
        }
      });

      // todo: 在api添加之后删除
      this.regiTypeData = [
        // {
        //   id: 1,
        //   registLevel: "急诊科",
        //   registCode: "jzk",
        //   registDefalut: "不默认",
        //   registCost: 2.0,
        //   registOrder: 1
        // },
        // {
        //   id: 2,
        //   registLevel: "急诊科2",
        //   registCode: "jzk2",
        //   registDefalut: "默认",
        //   registCost: 2.0,
        //   registOrder: 3
        // },
        // {
        //   id: 3,
        //   registLevel: "急诊科3",
        //   registCode: "jzk3",
        //   registDefalut: "不默认",
        //   registCost: 5,
        //   registOrder: 2
        // }
      ];
    },
    save() {
      this.addDialogVisible = false;
      this.regiType.registOrder = parseInt(this.regiType.registOrder);
      this.regiType.registCost = parseFloat(this.regiType.registCost);
      if (this.updateFlag) {
        /* update */
        console.log("send update:", this.regiType);
        update(this.regiType).then(response => {
          if (response.data.code == 0) {
            this.$message("修改成功");
            this.loadPageData();
          } else {
            this.$alter(response.data.msg, "修改失败", {
              confirmButtonText: "确定",
              callback: action => {
                this.$message({
                  type: "error",
                  message: `action:${action}`
                });
              }
            });
          }
        });
      } else {
        /* insert */
        console.log("send insert:", this.regiType);
        insert(this.regiType).then(response => {
          if (response.data.code == 0) {
            this.$message("添加成功");
            this.loadPageData();
          } else {
            this.$alter(response.data.msg, "添加失败", {
              confirmButtonText: "确定",
              callback: action => {
                this.$message({
                  type: "error",
                  message: `action:${action}`
                });
              }
            });
          }
        });
      }
    },
    handleEdit(index, row) {
      this.dialogTitle = "修改";
      this.addDialogVisible = true;
      // 切记此处不可直接赋值,直接赋值的话传递的是引用,先修改再新建的话,就会影响到之前被修改的行
      this.regiType = JSON.parse(JSON.stringify(row));
      this.updateFlag = true;
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除此项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          /*todo: 在这里填写在数据库中删除的语句 */
          let deleteParam = { ids: [row.id] };
          console.log("send deleteElem:", deleteParam);
          deleteElem(deleteParam).then(response => {
            if (response.data.code == 0) {
              this.$message("删除成功");
              this.loadPageData();
            } else {
              this.$alter(response.data.msg, "删除失败", {
                confirmButtonText: "确定",
                callback: action => {
                  this.$message({
                    type: "error",
                    message: `action:${action}`
                  });
                }
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // 更新页数
      this.loadPageData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPageNum = val;
      // 更新页数
      this.loadPageData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    batchRemoveButtonClicked() {
      this.$confirm("此操作将永久删除选中项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          /* 先封装成数组,再删除 */
          let removeIds = [];
          this.multipleSelection.forEach(item => {
            removeIds.push(item.id);
          });
          let deleteParam = {
            ids: removeIds
          };
          console.log("send deleteElem:", deleteParam);
          deleteElem(deleteParam).then(response => {
            if (response.data.code == 0) {
              this.$message("删除成功");
              this.loadPageData();
            } else {
              var failedids = "[";
              response.data.data.ids.forEach(item => {
                failedids += item + ",";
              });
              failedids += "]";
              response.data.data.ids;
              this.$alter(response.data.msg, failedids + " 失败", {
                confirmButtonText: "确定",
                callback: action => {
                  this.$message({
                    type: "error",
                    message: `action:${action}`
                  });
                }
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
</style>